package com.fingard.dsp.bank.reqrespobj.statement;

import com.fingard.dsp.bank.reqrespobj.RetAtsBase;
import com.fingard.dsp.bank.reqrespobj.statement.detail.RetAtsStatementDetail;
import com.fingard.xml.XmlTextWriter;

/**
 * 
 * 修复时间: 2017-08-18
 * 修复内容：
 * 		由于之前碰到的对账单只获取一次，而且对账单响应有汇总及明细信息。
 * 		<TotalNum>节点写在头部比较好，但随着对账单接口多样化，越来越多对账单需要多次获取，
 * 		而且返回对账单内容不包含汇总信息，无法先获取汇总信息，导致<TotalNum>无法写在头部。
 * 		现将多次获取对账单明细写完后最后再增加<TotalNum>节点。
 */
public class RetAtsStatement extends RetAtsBase {
	public String actNum = "";
	public String busDate = "";
	public String statementType = "";
	public String totalNum = "";
	
	public RetAtsStatement(ReqQryStatement p_req){
		retHeader = p_req.reqHeader;
		retXmlWriter = new XmlTextWriter();
		actNum = p_req.actNum;
		busDate = p_req.busDate;
		statementType = p_req.statementType;
	}

	private int writeRdCount = 0;
	public int getWrittenRdCount(){
		return writeRdCount;
	}
	
	boolean isWrited = false;
	public void isWritedTotalNum(String totalNum) throws Exception {
		if(!isWrited) {
			writeTotalNum(totalNum);
		}
		
		isWrited = true;
	}
	
	public boolean hasWrittenTotalNum = false;
	public void writeTotalNum(String totalNum) throws Exception{
		if(hasWrittenTotalNum) {
			throw new Exception("重复写入TotalNum");
		}
		retXmlWriter.writeElementString("TotalNum", totalNum);
		
		hasWrittenTotalNum = true;
	}
	
	public void writeRetDetail(RetAtsStatementDetail p_retDetail) throws Exception{
		writeRdCount++;
		if(writeRdCount == 1){
			if(!hasWrittenRespCode){
				writeRespCode("0");
			}
			if(!hasWrittenRespInfo){
				writeRespInfo("");
			}
			retXmlWriter.writeElementString("ActNum", actNum);
			retXmlWriter.writeElementString("BusDate", busDate);
			retXmlWriter.writeElementString("StatementType", statementType);
		}
		
		retXmlWriter.writeStartElement("DetailRecord");
		p_retDetail.writeXE(retXmlWriter);
		retXmlWriter.writeEndElement();
	}
}
